//
//  AssayController.m
//  Nurse
//
//  Created by Sunny on 13-12-21.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "AssayController.h"
#import "AssayDetialController.h"
#import "PatientService.h"
#import "LisService.h"
@interface AssayController ()

@property(nonatomic,weak)IBOutlet UITableView *table;

@property (nonatomic,strong) NSMutableArray* reports;

@end

@implementation AssayController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.reports = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"化验";
    // Do any additional setup after loading the view from its nib.
    //#import "PatientService.h"
    NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
    
    self.pInfoView.txtPatientName.text = [patient valueForKey:@"姓名"];
    NSString* pid = [patient valueForKey:@"住院号"];
    self.pInfoView.txtPatientID.text = [NSString stringWithFormat:@"住院号:%@",pid];
    self.pInfoView.txtBedNo.text = [NSString stringWithFormat:@"床位号:%@",[patient valueForKey:@"床位号"]];
    NSString*xb= [patient valueForKey:@"性别"];
    if ([xb isEqualToString:@"女"]) {
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-female2.png"];
    }
    else{
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-male2.png"];
    }
    
    __weak AssayController* this = self;
    
    [[LisService sharedInstance]queryLisReportForPID:pid withSuccessHandler:^(id result) {
        [this.reports removeAllObjects];
        [this.reports addObjectsFromArray:result];
        [this.tableView reloadData];
    } andFailHandler:^(NSError *error, NSString *title) {
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 57.f;
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.reports count];
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellEditingStyleNone;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identify = @"Cell";
    
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:identify];
    if (!cell ) {
       // NSArray *ibs = [[NSBundle mainBundle] loadNibNamed:@"AdviceDetailCell" owner:self options:nil];
      //  cell = (AdviceDetailCell *)[ibs objectAtIndex:0];
        // cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
       // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSInteger index = [indexPath row];
    NSDictionary* record = [self.reports objectAtIndex:index];
    NSString* title = [record valueForKey:@"项目名称"];
    NSString* date = [record valueForKey:@"申请日期"];
    NSString* status = [record valueForKey:@"处理状态"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@-%@-%@",date,title,status];
    
    cell.textLabel.font=[UIFont systemFontOfSize:16];
    //    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    return  cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = [indexPath row];
    NSDictionary* record = [self.reports objectAtIndex:index];
    NSString* _id = [record valueForKey:@"申请单号"];
    
    AssayDetialController *vc = [[AssayDetialController alloc] initWithNibName:@"AssayDetialController" bundle:nil];
    vc.applyID = _id;
    vc.Nstitle= [record valueForKey:@"项目名称"];
    NSString*year=[[record valueForKey:@"申请日期"] substringToIndex:4];
    NSString*month=[[record valueForKey:@"申请日期"] substringWithRange:NSMakeRange(5,2)];
    NSString*day=[[record valueForKey:@"申请日期"] substringFromIndex:7];
    vc.Nstime=[NSString stringWithFormat:@"%@-%@-%@",year,month,day];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
